package TestPaperSystem.controller;


import TestPaperSystem.Dto.PageUtils_VO;
import TestPaperSystem.Dto.PaperVo;
import TestPaperSystem.entity.Exam;
import TestPaperSystem.entity.ExamConfig;
import TestPaperSystem.entity.ExamLimit;
import TestPaperSystem.service.ExamConfigService;
import TestPaperSystem.service.ExamLimitService;
import TestPaperSystem.service.ExamService;
import TestPaperSystem.utils.R;
import TestPaperSystem.utils.ResponseUtils_R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    ExamService examService;

    @Autowired
    ExamConfigService examConfigService;

    @Autowired
    ExamLimitService examLimitService;

    @PostMapping("/page")
    public R getExamList(@RequestBody PageUtils_VO page){
        try{
            return ResponseUtils_R.success(examService.getListByExam(page));
        }catch (Exception e){
            return ResponseUtils_R.error("error");
        }

    }

    @PostMapping("/save")
    public R saveData(@RequestBody Exam exam){
        try{
            return ResponseUtils_R.success(examService.saveData(exam));
        }catch (Exception e){
            return ResponseUtils_R.error("error");
        }

    }

    @PostMapping("/getById")
    public R getExamById(@RequestBody PageUtils_VO page){
        try{
            return ResponseUtils_R.success(examService.getById(page.getId()));
        }catch (Exception e){
            return ResponseUtils_R.error("error");
        }
    }

    @PostMapping("/getSelfList")
    public R getExamByUserId(@RequestBody PageUtils_VO page){
        try{
            return ResponseUtils_R.success(examService.getSelfList(page));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtils_R.error("error");
        }
    }


    @PostMapping("/setConfig")
    public R saveExamConfig(@RequestBody ExamConfig examConfig){
        Boolean state = false;
        try {
            state = examConfigService.saveData(examConfig);
        }catch (Exception e){
            return ResponseUtils_R.error("失败");
        }
        return ResponseUtils_R.success(state);
    }

    @PostMapping("/getConfig")
    public R getExamConfig(@RequestBody PageUtils_VO page){
        ExamConfig state;
        try {
            state = examConfigService.getConfigByExamId(page.getId());
        }catch (Exception e){
            return ResponseUtils_R.error(e.getMessage());
        }
        return ResponseUtils_R.success(state);
    }

    @PostMapping("/getExamIsOpenCamera")
    public R isOpenCamera(@RequestBody PageUtils_VO page){
        Page page_db = null;
        try {
            page_db = examService.getExamAndConfig(page);
        }catch (Exception e){
            return ResponseUtils_R.error("传入参数有误");
        }
        return ResponseUtils_R.success(page_db);
    }

    @GetMapping("/getExamLimit/{examId}")
    public R getExamLimit(@PathVariable String examId){
        List<ExamLimit> examLimits = null;
        try {
            examLimits = examLimitService.list(new QueryWrapper<ExamLimit>().lambda().eq(ExamLimit::getExamId , examId));
        }catch (Exception e){
            return ResponseUtils_R.success(null);
        }
        return ResponseUtils_R.success(examLimits);

    }
    @PostMapping("/setExamLimit")
    public R setExamLimit(@RequestBody List<ExamLimit> examLimits) {
        return ResponseUtils_R.success(examLimitService.saveData(examLimits));

    }

    @PostMapping("/checkQualify")
    public R checkQualification(@RequestBody PaperVo paperVo){
        return ResponseUtils_R.success(examLimitService.getQualify(paperVo));
    }
}

